<? if(isset($post)) : ?>
    <!--facebook code-->
        <div id="fb-root"></div>
        <script>(function(d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) return;
            js = d.createElement(s); js.id = id;
            js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));</script>
    <!--facebook code end-->

    <div class="full_blog_wrapper">
       <div class="full_blog_wrapper_left">
            <?=Html::anchor(Request::$controller,'&larr; Back',array('class'=>'full_blog_back')) ?>
       </div>
        <div class="full_blog_wrapper_right">
            <div class="full_blog_info_wrapper" data-id="<?=$post['id']?>">
                <div class="full_blog_header">
                    <div class="full_blog_header_left"><?=$post['name']?></div>
                    <div class="full_blog_header_right"><?=$post['date']?></div>
                    <div class="clear"></div>
                </div>
                <div class="full_blog_main">
<!--                    <a href="#"><img src="--><?//=Model_Directory::itemImagePath($post,'blogFull')?><!--" alt=""></a>-->
                    <img src="<?=Model_Directory::itemImagePath($post,'blogFull')?>" alt="">
                    <div class="full_blog_text">
                        <?=$post['text2']?>
                     </div>
                </div>
                <div class="full_blog_bottom">
                    <div class="full_blog_bottom_left">
                        <div class="facebook_like">
                            <div class="fb-like" data-send="false" data-layout="button_count" data-width="50" data-show-faces="true" data-font="tahoma"></div>
                        </div>

                        <div class="twitter_like">

                            <a href="https://twitter.com/share" class="twitter-share-button">Tweet</a>
                            <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
                        </div>
                        <div class="clear"></div>

                    </div>

                    <div class="full_blog_bottom_right">
                        <span class="blog_views_icon"></span>
                        <span class="blog_views_count"><?=Model_Manager::getViews($post['id'])?></span>
                        <span class="blog_comments_icon"></span>
                        <span class="blog_comments_count"><?=count(Model_Manager::getCommentsByPostId($post['id']))?></span>
                    </div>
                    <div class="clear"></div>
                </div>
            </div>

            <div class="full_blog_comments_wrapper">
                <div class="full_blog_add_comment">
                    <? if(!isset($_SESSION['identity'])) : ?>
                        <input style="margin-bottom: 5px" type="text" name="name" placeholder="Name">
                    <? endif;?>
                    <textarea name="add_comment" placeholder="Press ENTER to add your comment..."></textarea>
                </div>

            <div class="full_blog_comment_wrapper">
                <? if(!empty($comments)) : ?>

                    <? foreach ($comments as $comment) : ?>

                        <? if($comment['isReply'] == 0) : ?>

                            <div class="full_blog_comment" data-id="<?=$comment['id']?>">
                                <div class="full_blog_comment_header">
                                    <div class="full_blog_comment_header_left">
                                        <div class="comment_author"><?=$comment['author']?></div>
                                        <div class="comment_date"><?=$comment['time']?></div>
                                        <div class="clear"></div>
                                    </div>
                                    <div class="full_blog_comment_header_right">
                                        <div class="comment_controls">

                                            <? if(isset($_SESSION['identity']) && $comment['author_id']==$_SESSION['identity']['id']) : ?>
                                                <span class="comment_delete"></span>
                                            <? endif;?>
                                            <span class="comment_reply"></span>
                                        </div>
                                    </div>
                                    <div class="clear"></div>
                                </div>

                                <div class="comment_text">
                                    <?=$comment['text']?>
                                </div>

                            </div>
                        <? endif; ?>

                        <? if($comment['hasReplies'] == 1) : ?>

                            <? foreach ($replies  as $reply) : ?>
                                <? if($reply['parent_id'] == $comment['id']) : ?>

                                    <div class="full_blog_comment_reply">
                                        <div class="full_blog_comment_header">
                                            <div class="full_blog_comment_header_left">
                                                <div class="comment_author"><?=$reply['author']?></div>
                                                <div class="comment_date"><?=$reply['time']?></div>
                                                <div class="clear"></div>
                                            </div>
                                            <div class="clear"></div>
                                        </div>

                                        <div class="comment_text">
                                            <?=$reply['text']?>
                                        </div>
                                    </div>

                                <? endif; ?>

                            <? endforeach; ?>

                        <? endif; ?>

                    <? endforeach; ?>

                <? endif; ?>
            </div>

            </div>
            <div style="text-align: center;display: none;" id="upBlock"><img src="/images/loader.gif" alt=""/></div>
        </div>
    </div>

    <script type="text/javascript">

        //comments controls
        (function($){

            $('.full_blog_comment').live('mouseover mouseout', function(event) {
                var self = $(this),
                    controls = self.find('.comment_controls');

                if (event.type == 'mouseover') {
                    controls.css({'display':'block'});
                } else {
                    controls.css({'display':'none'});
                }
            });

        })(jQuery);

        //add comment
        (function($){
            $('.full_blog_add_comment textarea').live('keypress',function(event){
                if(event.keyCode === 13 && $(this).val().trim() !==''){
                    var self = $(this),
                        comment = self.val(),
                        author  = self.parent().find('input').val(),
                        postId = self.parent().parent().parent().find('.full_blog_info_wrapper').data('id');

                    loader(self.parent(),'show',-34,1);

//                    self.prop('disabled',true);
                    self.parent().find('input').val('').trigger('focus').blur();
                    self.val('').blur();
                    $.ajax({
                        url     : '/blog/addcomment/',
                        type    : 'POST',
                        dataType: 'json',
                        data    : {comment:comment,postId:postId,author:author},
                        success : function(data){


                            cont = '<div class="full_blog_comment" style="display: none" data-id="'+data.success.comment.id+'">';
                            cont+= '<div class="full_blog_comment_header">';
                            cont+= '<div class="full_blog_comment_header_left">';
                            cont+= '<div class="comment_author">'+data.success.comment.author+'</div>';
                            cont+= '<div class="comment_date">'+data.success.comment.time+'</div>';
                            cont+= '<div class="clear"></div>';
                            cont+= '</div>';
                            cont+= '<div class="full_blog_comment_header_right">';
                            cont+= '<div class="comment_controls">';
                            if(data.success.deletion !== false){
                                cont+= '<span class="comment_delete"></span>';
                            }
                            cont+= '<span class="comment_reply"></span>';
                            cont+= '</div>';
                            cont+= '</div>';
                            cont+= '<div class="clear"></div>';
                            cont+= '</div>';
                            cont+= '<div class="comment_text">'+data.success.comment.text+'</div>';
                            cont+= '</div>';

                            $('.full_blog_comment_wrapper').prepend(cont);
                            $('.full_blog_comment_wrapper').find('.full_blog_comment[data-id="'+data.success.comment.id+'"]').slideDown();

                            //change comments count
                            var commentsCount = self.parent().parent().parent().find('.blog_comments_count'),
                                oldCount = commentsCount.text(),
                                newCount = parseInt(oldCount) +1;

                            commentsCount.text(newCount);

                        },
                        complete : function(){
                            loader(self.parent(),'hide');
//                            self.prop('disabled',false);
                        }
                    });

                    return false;
                }
            });
        })(jQuery);

        //add reply form
        (function($){
            $('.comment_reply').live('click',function(e){
                var self = $(this),
                commentBlock = self.parent().parent().parent().parent(),
                commentText = commentBlock.find('.comment_text');

                var authorVis = ($('.full_blog_add_comment').find('input').length > 0) ? "<input style='margin-bottom: 5px' type='text' placeholder='Name'/>" : "";

                if(!self.hasClass('active')){
                    $('.temp_div').slideUp(500).remove();
                    commentText.after("<div style='display: none;position: relative;' class='temp_div'>"+authorVis+"<textarea style='height: 3em;width: 100%' placeholder='Enter your reply'></textarea></div>");
                    commentText.next().slideDown(500);
                    //ie placeholder firing
                    commentText.next().find('input,textarea').placeholder();


                    self.addClass('active');
                }
                else{
                    commentText.next().slideUp(500);
                    self.removeClass('active');
                }

            })
        })(jQuery);

        //add Reply to comment
        (function($){
            $('.temp_div textarea').live('keypress',function(event){
                if(event.keyCode === 13 && $(this).val().trim() !==''){


                    var self = $(this),
                        comment = self.val(),
                        author = self.parent().find('input').val(),
                        postId = self.parent().parent().parent().parent().parent().parent().find('.full_blog_info_wrapper').data('id'),
                        parentId = self.parent().parent().data('id');

                    loader(self.parent(),'show',-34,1);
                    self.val('').blur().prop("disabled", true);
                    self.parent().find('input').val('').blur().prop('disabled',true);


                    $.ajax({
                        url     : '/blog/addreply/',
                        type    : 'POST',
                        dataType: 'json',
                        data    : {comment:comment,postId:postId,parentId:parentId,author:author},
                        success : function(data){
                            cont = '';
                            cont = '<div class="full_blog_comment_reply" style="display: none">';
                            cont+= '<div class="full_blog_comment_header">';
                            cont+= '<div class="full_blog_comment_header_left">';
                            cont+= '<div class="comment_author">'+data.success.comment.author+'</div>';
                            cont+= '<div class="comment_date">'+data.success.comment.time+'</div>';
                            cont+= '<div class="clear"></div>';
                            cont+= '</div>';
                            cont+= '<div class="clear"></div>';
                            cont+= '</div>';
                            cont+= '<div class="comment_text">'+data.success.comment.text+'</div>';
                            cont+= '</div>';

                            var parentComm = self.parent().parent();

                            $('.temp_div').slideUp(500);

                            parentComm.after(cont);
                            parentComm.next('.full_blog_comment_reply').slideDown(500);

                            //change comment count
                            var commentsCount = self.parent().parent().parent().parent().parent().find('.blog_comments_count'),
                                oldCount = commentsCount.text(),
                                newCount = parseInt(oldCount) +1;

                            commentsCount.text(newCount);

                            //remove attribute active from reply span
                            self.parent().parent().find('.comment_reply').removeClass('active');

                        },
                        complete : function(){
                            loader(self.parent(),'hide');
                        }
                    });
                    return false;
                }

            });


        })(jQuery);

        //comment delete

        (function($){
            $('.comment_delete').live('click',function(){
                var self = $(this),
                    comment = self.parent().parent().parent().parent(),
                    commentId = comment.data('id');

                $.ajax({
                    url     : '/blog/deletecomment/',
                    type    : 'POST',
                    dataType: 'json',
                    data    : {commentId:commentId},
                    success : function(data){
                        if(data.success){
                            window.location.reload();
                        }
                    },
                    complete: function(){

                    }
                });
            })
        })(jQuery);




        //loader function

        function loader($this, action,coordX,coordY) {
            var loader = new Image();
            loader.src = '/images/loader.gif';

            switch(action) {
                case 'show':
                    if($this.children('.loader').length == 0) {
                        var wBlock = $this.width();
                        var hBlock = $this.height();

                        var coordX = coordX || false,
                            coordY = coordY || false;

                        var c = '<div class="loader" style="position: absolute; display: none; opacity: 0.8"></div>';
                        $this.append(c);
                        var newLoader = new Image();
                        newLoader.src = loader.src;

                        $this.children('.loader').append(newLoader);

                        if(coordX === false || coordY === false){
                            $this.children('.loader').css({
                                'top': (hBlock-newLoader.height > 0) ? (hBlock-newLoader.height)/2 : 0,
                                'left': (wBlock-newLoader.width > 0) ? (wBlock-newLoader.width)/2 : 0
                            }).fadeTo(100, 1);
                        }
                        else{
                            $this.children('.loader').css({
                                'top'   : coordY,
                                'left'  : coordX
                            }).fadeTo(100,1);
                        }
                    }
                    break;
                case 'hide':
                    $this.children('.loader').stop().fadeOut(100, function() {
                        $(this).remove();
                    });
                    break;
            }
        };


    </script>

<script type="text/javascript">

    //autoload content on scroll
    autoScroll("",$('.full_blog_comment_wrapper'),$('#upBlock'));
    //placeholder fix
    jQuery('input[placeholder], textarea[placeholder]').placeholder();

</script>

<? endif; ?>